<!-- 手机验证 -->
<template>
    <div class="page-box h100 display-flex czbj" :data-time="currentTime">
        <p class="title-label mb10">短信余额：{{ limit }}元
        </p>
        <dxtable @delfun="delfun" ref="dxtableref" :dataJson="dataJson" :portUrl="portUrl" :qx="qx" :formJson="formJson"
            :czFun="czFun" addurl="phoneyz/fg/hhbserr"   :headBtn="headBtnw"></dxtable>
    </div>
</template>
<script lang='ts' setup>
import { leixing } from "../../util/config";
import dxtable from "../../components/util/dxtable.vue";
import { dxget, transferTimeYMD, transferTimeYMDHm,jCode,zCode,downloadExl } from "../../util/index";
import { NButton } from "naive-ui"
import store from "../../store/index"
import { h, ref, reactive, computed } from "vue";
const limit = ref('--')
const dxtableref = ref()

let dataJson = [
    {
        title: "手机号码",
        key: "phone",
        search: true, // 是否在搜索栏展示
    },
    {
        title: "最新短信",
        key: "LatestSMS",
    }, {
        title: "短信更新时间",
        key: "ShortmessageTime",
    },
    {
        title: "状态",
        key: "state",
        render(row) {
            if (row.state == 0) {
                return '--'
            }
            if (row.state == 1) {
                let jjhhsa = new Date().getTime()
                let jjhhsb = new Date(row.expirationTime).getTime()
                const djsd = parseInt((jjhhsb - jjhhsa) / 1000)
                if (djsd < 0) {
                    row.state = 2
                }
                return '生效中(' + djsd + '/240)'
            }
            if (row.state == 2) {
                return '已过期'
            }
            if (row.state == 4) {
                return row.errMsg
            }
        }
    }, {
        title: "生成时间",
        key: "addTime",
        sorter: (row1, row2) => jjnnert(row1.addTime) - jjnnert(row2.addTime)
    }, {
        title: "使用时间",
        key: "serviceTime",
        sorter: (row1, row2) => jjnnert(row1.addTime) - jjnnert(row2.addTime)
    }, {
        title: "到期时间",
        key: "expirationTime",
        sorter: (row1, row2) => jjnnert(row1.addTime) - jjnnert(row2.addTime)
    },
];
const jjnnert = (dd)=>{
    return new Date(dd).getTime()
}
const qx = {
    DeletePermissionID: "phoneyzdel", //删除
    EditPermissionIdentifier: "phoneyzbj", // 编辑
    NewPermissionIdentifier: "phoneyzxz", // 新建
    ViewPermissionID: "phoneyzck", // 查看
};
// const portUrl = "dxgjAdmin/administrator?CharacterNameCode=" + leixing;
const portUrl = "phoneyz/kdjphonemsg";
let formJson = reactive([
    {
        title: "手机号码",
        key: "phone",
        type: "textarea",
        required: true, // 是否为必填
    }


]);

const czFun = {
    fun: (row, loading) => {
        return [
            h(
                NButton,
                {
                    type: "info",
                    quaternary: true,
                    onClick: async (e) => {
                        if (row.state == 2||row.state == 4) {
                            loading.value = true
                            const res = await dxget('phoneyz/fg/sfphone', {
                                id: row.id
                            })
                            loading.value = false
                            dxtableref.value.initDataExpose()
                        } else {
                            navigator.clipboard.writeText(import.meta.env.VITE_APIURL + 'index?id=' + jCode(row.id)+'&phone='+row.phone)
                            window.$message.success("复制成功！")
                        }

                    },
                },
                row.state == 2||row.state == 4 ? "释放号码" : "复制链接"
            ),
        ];
    },
};
const getlimit = async () => {
    const { data }: any = await dxget("phoneyz/fg/getlimit")
    limit.value = data.limit
}

const delfun = async (id) => {
    const res = await dxget('phoneyz/fg/sfphone', {
        id
    })
}
getlimit()


const currentTime = computed(() => {
    try {
        dxtableref.value.initDataExpose()
    } catch (e) {

    }
    return store.state.currentTime;
});

const headBtnw = ref([
  {
    name: "导出",
    jy: true,
    clickFun: async() => {
      const {data}:any = await dxget("phoneyz/jhhser/index")
      const lisuer = []
      data.map(a=>{
        const obj = {}
        obj["手机号码"]=a.phone|| '--'
        obj["最新短信"]=a.LatestSMS || '--'
        obj["短信更新时间"]=a.ShortmessageTime|| '--'
        obj["生成时间"]=a.addTime|| '--'
        obj["使用时间"]=a.serviceTime|| '--'
        obj["到期时间"]=a.expirationTime|| '--'
        let jhhbse  = "生效中"
  
        if(a.state==2){
            jhhbse  = "已过期"
        }
        obj["状态"]=jhhbse
        // if(a.state==0){
        //     obj["链接地址"]='--'
        // }else{
        //     obj["链接地址"]=import.meta.env.VITE_APIURL + 'index?id=' + jCode(a.id)+'&phone='+a.phone
        // }
        obj["链接地址"]=import.meta.env.VITE_APIURL + 'index?id=' + jCode(a.id)+'&phone='+a.phone
        lisuer.push(obj)
      })
      downloadExl(lisuer,"",new Date().getTime())
    },
  },
]);
</script>
<style scoped>

</style>